﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

class Inventory
{
    public string 规格 { get; set; }
    public int 产量 { get; set; }
    public DateTime 时间戳 { get; set; }
}

class Program
{
    static void Main()
    {
        string jsonData = @"[
            [{'keyevent': [{'eventCode': 'difficult', 'index': 2}, {'eventCode': 'focus', 'index': 2}, {'eventCode': 'thinking', 'index': 2}]}, 
             {'extevent': [{'eventCode': 'helpless', 'index': 2}, {'eventCode': 'worry', 'index': 2}, {'eventCode': 'emotion', 'index': 2}, {'eventCode': 'excite', 'index': 2}]}],
            [{'keyevent': [{'eventCode': 'difficult', 'index': 2}, {'eventCode': 'focus', 'index': 2}, {'eventCode': 'thinking', 'index': 2}]}, 
             {'extevent': [{'eventCode': 'helpless', 'index': 2}, {'eventCode': 'worry', 'index': 2}, {'eventCode': 'emotion', 'index': 2}, {'eventCode': 'excite', 'index': 2}]}],
            [{'keyevent': [{'eventCode': 'difficult', 'index': 2}, {'eventCode': 'focus', 'index': 2}, {'eventCode': 'thinking', 'index': 2}]}, 
             {'extevent': [{'eventCode': 'helpless', 'index': 2}, {'eventCode': 'worry', 'index': 2}, {'eventCode': 'emotion', 'index': 2}, {'eventCode': 'excite', 'index': 2}]}]
        ]";

        Dictionary<string,List<string>> eventDicts = new Dictionary<string,List<string>>();
        Dictionary<string, int> eventCodeCount = new Dictionary<string, int>();

        List<List<Dictionary<string, List<Event>>>> data = JsonConvert.DeserializeObject<List<List<Dictionary<string, List<Event>>>>>(jsonData);
        // Accessing the data

        foreach (var item in data)
        {
            foreach (var dict in item)
            {
                foreach (var key in dict.Keys)
                {
                    foreach (var ev in dict[key])
                    {
                        if (eventCodeCount.ContainsKey(ev.eventCode))
                        {
                            eventCodeCount[ev.eventCode]++;
                        }
                        else
                        {
                            eventCodeCount[ev.eventCode] = 1; //value set
                        }
                    }
                }
            }
        }

        foreach (var entry in eventCodeCount)
        {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }

        //foreach (var item in data)  //三条之间枚举
        //{
        //    // item represents List<Dictionary<string,List<Event>>>
        //    foreach (var dict in item)  //一条  {‘up’:[]} {}
        //    {
        //        // dict represents list[i]  = 一本字典
        //        Console.WriteLine(dict.Keys.ToString());
        //        foreach (var key in dict.Keys)
        //        {    
        //            //只有一个Key 这个Key
        //            Console.WriteLine(key);
        //            foreach (var innerDict in dict[key])
        //            {
        //                //foreach (var innerKey in innerDict.Keys)
        //                //{
        //                //    Console.WriteLine(innerKey + ": " + string.Join(", ", innerDict[innerKey]));
        //                //}
        //            }
        //        }
        //    }
        //}





        string final_result = @"Time {总体}{谓语1}{状态}的状态，{连接词}状态{状态趋势}，并伴随持续的{helpless}、明显的{worry}、明显的{emotion}。";
        int start_index = final_result.IndexOf("并伴随");
        string var1 = final_result.Substring(0, start_index - 1);
        string var2 = final_result.Substring(start_index + 3);


        final_result = final_result.Substring(0, start_index - 1) + final_result.Substring(start_index + 3);


        Console.ReadKey();

        //List<Inventory> inventoryList = GetInventoryData(); // 假设这里获取了库存数据

        //var query = from item in inventoryList
        //            group item by new
        //            {
        //                时间分类 = item.时间戳.Date == DateTime.Today ? "今日" : "其他时间",
        //                item.规格
        //            } into g
        //            select new
        //            {
        //                时间分类 = g.Key.时间分类,
        //                规格 = g.Key.规格,
        //                总产量 = g.Sum(x => x.产量)
        //            };

        //foreach (var result in query)
        //{
        //    Console.WriteLine($"时间分类: {result.时间分类}, 规格: {result.规格}, 总产量: {result.总产量}");
        //}


        string input = "time{totaldes}{predicate1}{status}的状态，{connect}状态{status_trend}，其中{predicate2}{keyevent}，并伴随{extevent}";

        // 定义要匹配的关键词
        string[] keywords = { "totaldes", "predicate1", "status", "connect", "status_trend", "predicate2", "frequence", "keyevent", "extevent" };

        // 使用正则表达式匹配大括号中的内容
        string pattern = @"\{([^{}]*)\}";
        MatchCollection matches = Regex.Matches(input, pattern);

        // 遍历匹配结果，并根据关键词提取相关部分
        foreach (Match match in matches)
        {
            string content = match.Groups[1].Value;
         //   if (keywords.Contains(content))
            {
                Console.WriteLine(content + ": " + match.Value);
            }
        }
    }

    static List<Inventory> GetInventoryData()
    {
        // 这里可以是从数据库、文件或其他数据源中获取库存数据的逻辑
        // 这里只是一个示例，实际情况需要根据你的数据源进行调整
        return new List<Inventory>
        {
            new Inventory { 规格 = "规格1", 产量 = 100, 时间戳 = DateTime.Today },
            new Inventory { 规格 = "规格1", 产量 = 120, 时间戳 = DateTime.Today },
            new Inventory { 规格 = "规格2", 产量 = 150, 时间戳 = DateTime.Today },
              new Inventory { 规格 = "规格2", 产量 = 150, 时间戳 = DateTime.Today },
            new Inventory { 规格 = "规格1", 产量 = 200, 时间戳 = DateTime.Today.AddDays(-1) },
            new Inventory { 规格 = "规格1", 产量 = 200, 时间戳 = DateTime.Today.AddDays(-1) },
            new Inventory { 规格 = "规格2", 产量 = 120, 时间戳 = DateTime.Today.AddDays(-1) }
        };
    }
}

public class Event
{
    public string eventCode { get; set; }
    public int index { get; set; }
}